package com.dfbz.controller;

import com.dfbz.entity.Meeting;
import com.dfbz.entity.PageEntity;
import com.dfbz.service.MeetService;
import com.dfbz.utils.DateConverter;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtilsBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

@WebServlet("/meeting/*")
public class meetingServlet extends baseServlet {
    /**
     * 模糊查询+分页
     *
     * @param request
     * @param response
     */
    public MeetService meetService = new MeetService();

    public void findPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        Map<String, String> param = getParam(request);

        //获取    前端传来的 当前页 页码 和标题 进行模糊查询
        Integer currPage = Integer.parseInt(param.get("currPage"));

        String title = param.get("title");

        //会议状态默认为-1

        Integer status = -1;

        if (!"".equals(param.get("status"))) {
            status = Integer.parseInt(param.get("status"));
        }

        //会议分页
        PageEntity<Meeting> pageData = meetService.findPage(currPage, title, status);

        request.setAttribute("pageData", pageData);
        request.setAttribute("currPage", currPage);
        request.setAttribute("title", title);
        request.getRequestDispatcher("/html/meeting.jsp").forward(request, response);

    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {

        // 前台提交的参数
        Map<String, String> param = getParam(request);

        Meeting meeting = new Meeting();

        // 日期映射错误
//        BeanUtils.populate(meeting, param);

        // 类型转换工具类
        ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();


    /*  注册日期转换器,负责转换Date类型
        参数1: 处理转换逻辑的核心类(必须是Converter的子类)
        参数2: 最终要转换成什么类型
    */
        DateConverter dateConverter = new DateConverter();
        convertUtilsBean.register(dateConverter, Date.class);

        // bean对象映射工具类
        BeanUtilsBean beanUtils = new BeanUtilsBean(convertUtilsBean);

        //使用注册了类型转换工具的映射工具类来进行属性映射
        beanUtils.populate(meeting, param);

        meetService.save(meeting);

        response.sendRedirect("/meeting/findPage?currPage=1");

    }

    /**
     * 会议详情
     * @param request
     * @param response
     */
    public void meetingDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        Map<String, String> param = getParam(request);
        long id = Long.parseLong(param.get("id"));
        //查询会议详情
        Meeting meeting=meetService.findByid(id);
        //需要参加会议的用户

        //实到人数
        String[] joinUser = meeting.getMakeUser().split(","+" ");
        Integer realCount=meetService.countJoinMeetingMember(id);

        //未到人数
        Integer noCome=joinUser.length-realCount;

        //查看我是否可以参加这次的会议
        Boolean flag=meetService.isJoinMeeting(loginUser.getId(),id);

        request.setAttribute("meeting",meeting);
        request.setAttribute("flag",flag);
        request.setAttribute("noCome",noCome);
        request.setAttribute("realCount",realCount);
        request.setAttribute("joinUser",joinUser.length);
        request.setAttribute("isJoin", Arrays.asList(joinUser).contains(loginUser.getId()+""));
        request.getRequestDispatcher("/html/meeting_detail.jsp").forward(request,response);
    }

    public void joinMeeting(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        Map<String, String> param = getParam(request);

        long id = Long.parseLong(param.get("id"));

        //查询是否参加过这个会议
        Boolean joinMeeting = meetService.isJoinMeeting(loginUser.getId(), id);
        if(joinMeeting){
            meetService.unJoin(loginUser.getId(),id);
        }else {
            meetService.CanJoin(loginUser.getId(),id);
        }
        response.sendRedirect("/meeting/meetingDetail?id="+id);
    }

}
